package com.qin.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.dto.*;
import com.qin.mall.result.ResultCode;
import com.qin.mall.service.OmsOrderService;
import com.qin.mall.domain.OmsOrder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理
 * @author Leisure
 * create 2022 2022/11/9 18:53
 **/
@RestController
@RequestMapping("/omsorder")
public class OmsOrderController {
    
    @Autowired
    private OmsOrderService orderService;

    /**
     * 分页查询所有订单
     * @return
     */
    @GetMapping("/list")
    public CommonResult<MyBatisPlusPage<OmsOrder>> list(OmsOrderQueryParam queryParam,
                                                        @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
       Page<OmsOrder> orders = orderService.list(pageNum, pageSize);
       return CommonResult.success(MyBatisPlusPage.mpPage(orders));
    }

    /**
     * 条件查询订单并分页
     * @param pageNum
     * @param pageSize
     * @param queryParam
     * @return
     */
    @PostMapping("/listQuery")
    @ResponseBody
    public CommonResult<MyBatisPlusPage<OmsOrder>> listQuery(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                            @RequestBody OmsOrderQueryParam queryParam){
        Page<OmsOrder> orders = orderService.list(queryParam,pageNum,pageSize);
        return CommonResult.success(MyBatisPlusPage.mpPage(orders));
    }

    /**
     * 批量发货
     * @param deliveryParamList
     * @return
     */
    @PostMapping("/update/delivery")
    public CommonResult delivery(@RequestBody List<OmsOrderDeliveryParam> deliveryParamList){
        int count = orderService.delivery(deliveryParamList);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 批量关闭订单
     * @param note
     * @param ids
     * @return
     */
    @PostMapping("/update/close")
    public CommonResult close(@RequestParam("note") String note,
                        @RequestParam("ids") List<Long> ids){
        int count = orderService.close(ids, note);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 批量删除订单
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam("ids") List<Long> ids){
        int count = orderService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 获取指定订单详情
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    @ResponseBody
    public CommonResult<OmsOrderDetail> detail(@PathVariable Long id){
        OmsOrderDetail order = orderService.detail(id);
        return CommonResult.success(order);
    }

    /**
     * 修改订单收货人信息
     * 测试数据：
     * {
     *     "orderId":30,
     *     "receiverName":"覃剑",
     *     "receiverPhone":1987635200,
     *     "receiverPostCode":613200,
     *     "receiverDetailAddress":"广师大",
     *     "receiverProvince":"广东省",
     *     "receiverCity":"广州市",
     *     "receiverRegion":"天河区",
     *     "status":3
     * }
     * @param receiverInfoParam
     * @return
     */
    @PostMapping("/update/receiverInfo")
    public Object updateReceiverInfo(@RequestBody OmsReceiverInfoParam receiverInfoParam){
        int result = orderService.updateReceiverInfo(receiverInfoParam);
        if (result>0){
            ResultCode success = ResultCode.SUCCESS;
            return  success.getMessage();
        }else{
            ResultCode failed = ResultCode.FAILED;
            return failed;
        }
    }

    /**
     * 修改订单费用信息
     * 测试数据:
     * {
     *     "orderId":30,
     *     "freightAmount":100,
     *     "discountAmount":100,
     *     "status":4
     * }
     * @param moneyInfoParam
     * @return
     */
    @PostMapping("/update/moneyInfo")
    public Object updateReceiverInfo(@RequestBody OmsMoneyInfoParam moneyInfoParam){
        int result = orderService.updateMoneyInfo(moneyInfoParam);
        if (result>0){
            ResultCode success = ResultCode.SUCCESS;
            return  success.getMessage();
        }else{
            ResultCode failed = ResultCode.FAILED;
            return failed;
        }
    }

    /**
     * 修改订单备注 /这里原来是用post请求，但是我觉得用get更合理
     * @param id
     * @param note
     * @param status
     * @return
     */
    @PostMapping("/update/note")
    public CommonResult updateNote(@RequestParam("id") Long id,
                             @RequestParam("note") String note,
                             @RequestParam("status") Integer status){
        int count = orderService.updateNote(id, note, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();

    }
}
